import pandas as ps
from sqlalchemy import create_engine
from config import DB_URL
from loguru import logger as log
from check_log_grab.common import save_json, DateEncoder


def read_db(sql: str):
    engine = create_engine(DB_URL)
    return ps.read_sql_query(sql, engine, coerce_float=False)


def insert_batch(obj, data, err_log):
    """
    批量导入
    :param obj: sqlalchemy.modes 对象
    :param data: 列表字典对象
    :param err_log: 插入错误，保存文件、
    :return:
    """
    engine = create_engine(DB_URL)
    try:
        engine.execute(obj.__table__.insert(), data)
    except Exception as e:
        # 需要添加错误，回退数据机制
        log.error(e)
        save_json(err_log, data, class_type=DateEncoder)
